﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using System.Net;

namespace Web4Comment
{
    public partial class news : System.Web.UI.Page
    {      
        public string url;


        protected void Page_Load(object sender, EventArgs e)
        {
            //Session["username"] = "username12345";

            //WebClient sHttpWebClinet = new WebClient();
            //string strWebText = System.Text.Encoding.Default.GetString(sHttpWebClinet.DownloadData("http://www.sina.com"));
            //newsContentIframe.InnerText = strWebText;
;

            if ((String)Session["username"]==null || (String)Session["username"] == "")
            {
                Response.Redirect("./logon.aspx");
            }

            if (!IsPostBack)
            {
                url = Request.QueryString["url"];
                newsContentIframe.Attributes["src"] = url;
                urlTextBox.Text = url;
                List<UnlessComment.Model.FriendDetail> friendList = Service.FriendsService.getMyFriends((String)Session["username"]);

                friendDropDownList.DataSource = friendList;
                friendDropDownList.DataTextField = "friend_username";
                friendDropDownList.DataValueField = "friend_username";
                friendDropDownList.DataBind();

                long x = Web4Comment.Service.ChatTimeService.logNewsStartTime((Int32)Session["user_id"], DateTime.Now, Web4Comment.Service.NewsService.getNewsId(url));
                logID.Text = x.ToString();
            }

            

            List<UnlessComment.Model.CommentDetail> commentsList = Service.CommentService.getNewsComments(urlTextBox.Text);
            Collection<UnlessComment.Model.CommentDetail> comments = new Collection<UnlessComment.Model.CommentDetail>();


            if (commentsList == null || commentsList.Count == 0)
            {
                if (urlTextBox.Text != null && urlTextBox.Text != "")
                {
                    if (Service.NewsService.getSingleNews(urlTextBox.Text) == null)
                    {
                        commentsdl.DataSource = comments;
                        commentsdl.DataBind();
                        addLabel.Text = "还没有人分享该新闻，分享吧";
                        addPanel.Visible = true;
                        commentPanel.Visible = false;
                    }
                    else
                    {
                        commentsdl.DataSource = comments;
                        commentsdl.DataBind();
                        addLabel.Text = "还没有人分享该新闻，分享吧";
                        addPanel.Visible = false;
                        commentPanel.Visible = true;
                    }
                }
                else
                {
                    //commentPanel.Visible = true;
                }
                return;
            }

            for (int i = 0; i < commentsList.Count; i++)
            {
                comments.Add(commentsList[i]);
            }

            commentsdl.DataSource = comments;
            commentsdl.DataBind();
            addPanel.Visible = false;
            commentPanel.Visible = true;
        }

        protected void BookImageButton_Click(object sender, ImageClickEventArgs e)
        {
            url = urlTextBox.Text;
            newsContentIframe.Attributes["src"] = url;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            url = urlTextBox.Text;
            newsContentIframe.Attributes["src"] = url;
        }

        protected void addButton_Click(object sender, EventArgs e)
        {
            string content = "";
            WebClient sHttpWebClinet = new WebClient();
            string strWebText = System.Text.Encoding.Default.GetString(sHttpWebClinet.DownloadData(urlTextBox.Text));
            Int32 i1 = strWebText.IndexOf("<title>") + 7;
            Int32 i2 = strWebText.IndexOf("</title>");
            if (i2 - i1 <= 0)
                content = "unknown";
            else
                content = strWebText.Substring(i1, i2 - i1);
            if (Service.NewsService.addNews(urlTextBox.Text, content) <= 0)
            {
                addLabel.Text = "分享失败，请重新分享";
            }
            else
            {
                addLabel.Text = "分享成功，请评论并分享给好友";
                addPanel.Visible = false;
                commentPanel.Visible = true;
            }
            
        }

        protected void commentButton_Click(object sender, EventArgs e)
        {
            if (toLabel.Text == "")
            {
                Service.CommentService.commentNews((String)Session["username"], newsContentIframe.Attributes["src"].ToString(), commentTextBox.Text);
            }
            else
            {
                int commentId = Convert.ToInt32(toLabel.Text);
                int index = commentTextBox.Text.LastIndexOf(":");
                Service.CommentService.commentCommentary((String)Session["username"], newsContentIframe.Attributes["src"].ToString(), commentId, commentTextBox.Text.Substring(index + 1));
            }

            Response.Redirect("./news.aspx?url=" + newsContentIframe.Attributes["src"].ToString());
        }

        protected void HandleReply(object sender, EventArgs e)
        {
            int commentId = Convert.ToInt32(((LinkButton)sender).CommandArgument);
            toLabel.Text = "" + commentId;
            commentTextBox.Text = "引用"+commentId+"\"" + Service.CommentService.getCommentById(commentId).content+"\":";
            //commentTextBox.Text = "回复:/" + ((LinkButton)sender).CommandArgument + "\n+aa";
        }

        protected void HandleComment(object sender, EventArgs e)
        {
            toLabel.Text = "";
            commentTextBox.Text = "";
            //commentTextBox.Text = "回复:/" + ((LinkButton)sender).CommandArgument + "\n+aa";
        }

        protected void recommendButton_Click(object sender, EventArgs e)
        {
            if (commentPanel.Visible)
            {
                string name = friendDropDownList.SelectedValue;
                string name1 =friendDropDownList.SelectedItem.Text;
                int x = friendDropDownList.SelectedIndex;
                Service.NewsService.recommendNews((String)Session["username"], friendDropDownList.SelectedValue, newsContentIframe.Attributes["src"]);
            }
        }

        protected void startButton_Click(object sender, EventArgs e)
        {

        }

        protected void endButton_Click(object sender, EventArgs e)
        {
            Web4Comment.Service.ChatTimeService.logEndTime(System.Int64.Parse(logID.Text), DateTime.Now);
        }
    }
}